<template>
<div class="product-add">
    <!--form表单-->
    <el-form size="small" ref="form" :model="form" label-width="200px">
      <!--基本信息-->
      <Basic></Basic>

      <!--活动时间-->
      <Datetime></Datetime>

      <!--其它设置-->
      <Other></Other>
    </el-form>

    <!--提交-->
    <div class="common-button-wrapper">
      <el-button size="small" @click="cancelFunc">{{$t('plus.cancel_text')}}</el-button>
      <el-button
        size="small"
        type="primary"
        @click="onSubmit"
        :disabled="loading"
        >{{$t('plus.submit')}}</el-button
      >
    </div>
  </div>
</template>

<script>
import BargainApi from "@/api/bargain.js";
import Basic from "./part/Basic.vue";
import Datetime from "./part/Datetime.vue";
import Product from "./part/Product.vue";
import Other from "./part/Other.vue";
import { mergeTable } from "@/utils/table.js";
export default {
  components: {
    Basic,
    Datetime,
    Product,
    Other
  },
  data() {
    return {
      /*是否正在加载*/
      loading: false,
      /*form表单对象*/
      form: {
        /*活动名称*/
        title: "",
        /*广告图片ID*/
        imageId: 0,
        /*活动广告图*/
        filePath: "",
        /*活动时间*/
        startTime: "",
        endTime: "",
        /*砍价有效期*/
        togetherTime: 0,
        /*状态*/
        status: 1,
        /*购买条件*/
        conditions: 1,
        /*活动排序*/
        sort: 1,
        joinEndTime: "",
        activeDate: [],
        /*商品源数据*/
        tableData: [],
        /*商品列表*/
        productList: []
      }
    };
  },
  provide: function () {
    return {
      form: this.form,
      type: "edit"
    };
  },
  created() {
    this.form.bargainActivityId = this.$route.query.bargain_activity_id;
    this.getData();
  },
  methods: {
    /*获取编辑数据*/
    getData() {
      let self = this;
      self.loading = true;
      BargainApi.editActive({
        bargainActivityId: self.form.bargainActivityId
      }, true).then(res => {
        let tempForm = res.data;
        tempForm.bargainActivityId = self.form.bargainActivityId;
        tempForm.activeDate = [];
        tempForm.activeDate[0] = tempForm.startTime;
        tempForm.activeDate[1] = tempForm.endTime;
        Object.assign(self.form, tempForm);
        self.loading = false;
      }).catch(error => {
        self.loading = false;
      });
    },
    /*修改*/
    onSubmit() {
      let self = this;
      self.$refs.form.validate(valid => {
        if (valid) {
          let params = self.form;
          params.startTime = params.activeDate[0];
          params.endTime = params.activeDate[1];
          self.loading = true;
          BargainApi.saveActive(params, true).then(data => {
            ElMessage({
              message: $t("plus.bargain_edit_success"),
              type: "success"
            });
            self.$router.push("/plus/bargain/index");
          }).catch(error => {});
        }
      });
    },
    /*取消*/
    cancelFunc() {
      this.$router.back(-1);
    }
  }
};
</script>

<style lang="scss" scoped>
.product-add {
  padding-bottom: 50px;
}

.tips {
  color: #ccc;
}

.img {
  margin-top: 10px;
}
</style>

